Package xunome.game.multiplayerg

Source Code of xunome.game.multiplayerg.BluetoothConnectionServer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package xunome.game.multiplayerg;

import java.io.IOException;
import xunome.xUnoME;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
/**
*
* @author Sem iNick
*/

//TODO Remover servidor do Database de serviços quando server for fechado
public class BluetoothConnectionServer extends ConnectionServer implements Runnable {

    public static final String UUID = "22041991191119931313195617051961";
    private LocalDevice local;
    //private Thread t;
   
    public BluetoothConnectionServer(xUnoME mid, String name) {

        super(mid, name);
        try {

            local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);

            setManager(new MultiPlayerManager(this, mid, name));

            new Thread(this).start();
           
            getManager().showChatForm();
        } catch (Exception e) {

            Alert a = new Alert("Error", "Server couldn't be initialized.",
                                null, AlertType.ERROR);
            a.setTimeout(3000);
            mid.getLCD().setCurrent(a, new xunome.MultiPlayerForm(mid));
        }
    }

    public void run() {

        StreamConnectionNotifier localServer = null;
        try {
           
            localServer = (StreamConnectionNotifier) Connector.open("btspp://localhost:"
                                                + UUID + ";name=" + getName());
            setLocalServer(localServer);
            startListening();
        } catch (IOException ex) {

            //#if DEBUG
            System.out.println("Error in create server.");
            ex.printStackTrace();
            //#endif
        }
    }

    public void pauseListening() {
       
        try {

            //#if DEBUG
            System.out.println("pause Bluetooth");
            //#endif
            local.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE);
        } catch (Exception ex) {

            //#if DEBUG
            ex.printStackTrace();
            //#endif
        }
        super.pauseListening();
    }

    public void resumeListening() {

        try {

            local.setDiscoverable(DiscoveryAgent.GIAC);
        } catch (BluetoothStateException bse) {

            //#if DEBUG
            System.out.println("Error Resume");
            //#endif
        }
        super.resumeListening();
    }

    public void stopListening() {

        try {

            local.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE);
        } catch (Exception e) {

            //#ifdef DEBUG
            e.printStackTrace();
            //#endif
        }
        super.stopListening();
    }
}
TOP

Related Classes of xunome.game.multiplayerg.BluetoothConnectionServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.